Coding Quiz 5

Author

Matthew Maslow

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(palmerpenguins)
penguins
# A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 2 more variables: sex <fct>, year <int>

Question 1

plot1 <-
  ggplot(data = penguins, aes(x = bill_depth_mm, y = bill_length_mm,
                              label = sex)) + # added label in her to let R know Im gonna use it below
    geom_point(aes(colour = species)) +
    scale_colour_viridis_d(option = "B") +
    theme_minimal()
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
# now plug in plot object and set tooltip to sex
ggplotly(plot1, tooltip = "sex")

Question 2

penguins |> pull(species) |> unique()
[1] Adelie    Gentoo    Chinstrap
Levels: Adelie Chinstrap Gentoo
penguins_one_sp_island <- penguins |> 
  filter(island == "Biscoe") |>
  filter(species == "Adelie") |>
  filter(!is.na(sex))

ggplot(data = penguins_one_sp_island, aes(x = sex, y = bill_depth_mm)) +
  geom_jitter(width = 0.25) +
  labs(title = "Adelie Penguins on Biscoe") +
  theme_minimal()

penguins_one_sp_island |> group_by(sex) |>
  summarise(mean = mean(bill_depth_mm, na.rm = TRUE),
            sd = sd(bill_depth_mm, na.rm = TRUE)) 
# A tibble: 2 × 3
  sex     mean    sd
  <fct>  <dbl> <dbl>
1 female  17.7 1.09 
2 male    19.0 0.880
library(shiny)

ui <- fluidPage(
  titlePanel("Penguins"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "island",
                  label = "Island",
                  choices = c("Biscoe", "Dream", "Torgersen"),
                  selected = "Biscoe"),
      selectInput(inputId = "species",
                  label = "Species",
                  choices = c("Adelie", "Chinstrap", "Gentoo"),
                  selected = "Adelie"),
      sliderInput(inputId = "jitter",
                  label = "Jitter Width",
                  min = 0,
                  max = 0.25,
                  value = 0.125)
    ),
    mainPanel(
      plotOutput(outputId = "strip_plot"),
      tableOutput(outputId = "summary_table")
    )
  )
)

server <- function(input, output, session) {
  observe({
    penguins_filtered <- penguins %>%
      filter(island == input$island) %>%
      filter(!is.na(species))
    
    updateSelectInput(session, "species", choices = unique(penguins_filtered$species))
  })
  
  output$strip_plot <- renderPlot({
    penguins_one_sp_island <- penguins %>% 
      filter(island == input$island) %>%
      filter(species == input$species) %>%
      filter(!is.na(sex))

    ggplot(data = penguins_one_sp_island, aes(x = sex, y = bill_depth_mm)) +
      geom_jitter(width = input$jitter) +
      labs(title = paste(input$species,"Penguins on", input$island)) +
      theme_minimal()
  })
  
  output$summary_table <- renderTable({
    penguins_one_sp_island <- penguins %>% 
      filter(island == input$island) %>%
      filter(species == input$species) %>%
      filter(!is.na(sex))
    
    penguins_one_sp_island %>%
      group_by(sex) %>%
      summarise(mean = mean(bill_depth_mm, na.rm = TRUE),
                sd = sd(bill_depth_mm, na.rm = TRUE))
  })

}

shinyApp(ui = ui, server = server)
PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

Listening on http://127.0.0.1:8474